package com.example.whereismystuff.classes;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.widget.Toast;

public class ItemDataBaseAdapter {
	static final String DATABASE_NAME = "item.db";
	public static final String KEY_ID = "_id";
	static final int DATABASE_VERSION = 1;
	public static final int NAME_COLUMN = 1;
	// TODO: Create public field for each column in your table.
	// SQL Statement to create a new database.
	static final String DATABASE_CREATE = "create table "
			+ "ITEM"
			+ "( "
			+ "_id"
			+ " integer primary key autoincrement,"
			+ "USERNAME  text, ITEMNAME text, DESCRIPTION text, DATE text, FOUND text, BOOK text, ELECTRONIC text, PERSONAL text, ZIPCODE text, IMAGE text, LATITUDE text, LONGITUDE text); ";
	// Variable to hold the database instance
	public SQLiteDatabase db;
	// Context of the application using the database.
	private final Context context;
	// Database open/upgrade helper
	private ItemDatabaseHelper dbHelper;

	public ItemDataBaseAdapter(Context _context) {
		context = _context;
		dbHelper = new ItemDatabaseHelper(context, DATABASE_NAME, null,
				DATABASE_VERSION);
	}

	public ItemDataBaseAdapter open() throws SQLException {
		db = dbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		db.close();
	}

	public SQLiteDatabase getDatabaseInstance() {
		return db;
	}
	
	public void insertEntry(String userName, String itemName, String description, String date,
			String found, ArrayList<String> tags, String zipCode, String latitude, String longitude) {
		ContentValues newValues = new ContentValues();
		// Assign values for each row.
		newValues.put("USERNAME", userName);
		newValues.put("ITEMNAME", itemName);
		newValues.put("DESCRIPTION", description);
		newValues.put("DATE", date);
		newValues.put("FOUND", found);
		if(tags != null) {
			newValues.put("BOOK", tags.get(0));
			newValues.put("ELECTRONIC", tags.get(1));
			newValues.put("PERSONAL", tags.get(2));
		}
		newValues.put("ZIPCODE", zipCode);
		newValues.put("LATITUDE", latitude);
		newValues.put("LONGITUDE", longitude);

		// Insert the row into your table
		db.insert("ITEM", null, newValues);
	}

	public void insertEntry(String userName, String itemName, String description, String date,
			String found, ArrayList<String> tags, String zipCode) {
		ContentValues newValues = new ContentValues();
		// Assign values for each row.
		newValues.put("USERNAME", userName);
		newValues.put("ITEMNAME", itemName);
		newValues.put("DESCRIPTION", description);
		newValues.put("DATE", date);
		newValues.put("FOUND", found);
		if(tags != null) {
			newValues.put("BOOK", tags.get(0));
			newValues.put("ELECTRONIC", tags.get(1));
			newValues.put("PERSONAL", tags.get(2));
		}
		newValues.put("ZIPCODE", zipCode);

		// Insert the row into your table
		db.insert("ITEM", null, newValues);
	}
	
	public void insertEntry(String userName, String itemName, String description, String date,
			String found, ArrayList<String> tags, String zipCode, String imageName,String latitude, String longitude) {
		ContentValues newValues = new ContentValues();
		// Assign values for each row.
		newValues.put("USERNAME", userName);
		newValues.put("ITEMNAME", itemName);
		newValues.put("DESCRIPTION", description);
		newValues.put("DATE", date);
		newValues.put("FOUND", found);
		if(tags != null) {
			newValues.put("BOOK", tags.get(0));
			newValues.put("ELECTRONIC", tags.get(1));
			newValues.put("PERSONAL", tags.get(2));
		}
		newValues.put("ZIPCODE", zipCode);
		newValues.put("IMAGE", imageName);
		newValues.put("LATITUDE", latitude);
		newValues.put("LONGITUDE", longitude);
		// Insert the row into your table
		db.insert("ITEM", null, newValues);
	}

	public void insertEntry(String userName, String itemName, String description, String date,
			String found, ArrayList<String> tags, String zipCode, Bitmap image) {

		ContentValues newValues = new ContentValues();
		// Assign values for each row.
		newValues.put("USERNAME", userName);
		newValues.put("ITEMNAME", itemName);
		newValues.put("DESCRIPTION", description);
		newValues.put("DATE", date);
		newValues.put("FOUND", found);
		if(tags != null) {
			newValues.put("BOOK", tags.get(0));
			newValues.put("ELECTRONIC", tags.get(1));
			newValues.put("PERSONAL", tags.get(2));
		}
		newValues.put("ZIPCODE", zipCode);
		if(image != null) {
			ByteArrayOutputStream stream = new ByteArrayOutputStream();
			Bitmap temp = image;
			temp.compress(Bitmap.CompressFormat.PNG, 100, stream);
			byte[] byteArray = stream.toByteArray();
			newValues.put("IMAGE", byteArray);
		}
		// Insert the row into your table
		db.insert("ITEM", null, newValues);
		// /Toast.makeText(context, "Reminder Is Successfully Saved",
		// Toast.LENGTH_LONG).show();
	}


	public int deleteEntry(String itemName) {
		// String id=String.valueOf(ID);
		String where = "ITEMNAME=?";
		int numberOFEntriesDeleted = db.delete("ITEM", where,
				new String[] { itemName });
		// Toast.makeText(context,
		// "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted,
		// Toast.LENGTH_LONG).show();
		return numberOFEntriesDeleted;
	}

	public boolean containsItem(String itemName) {
		Cursor cursor = db.query("ITEM", null, " ITEMNAME=?",
				new String[] { itemName }, null, null, null);
		return !(cursor.getCount() < 1);
	}

	public String getSingleEntryItemName(String userName) {
		Cursor cursor = db.query("ITEM", null, " USERNAME=?",
				new String[] { userName }, null, null, null);
		if (cursor.getCount() < 1) // UserName Not Exist
		{
			cursor.close();
			return "NOT EXIST";
		}
		cursor.moveToFirst();
		String itemName = cursor.getString(cursor.getColumnIndex("ITEMNAME"));
		cursor.close();
		return itemName;
	}

	public String getSingleEntryFound(String itemName) {
		Cursor cursor = db.query("ITEM", null, "ITEMNAME=?",
				new String[] { itemName }, null, null, null);
		if (cursor.getCount() < 1) // UserName Not Exist
		{
			cursor.close();
			return "NOT EXIST";
		}
		cursor.moveToFirst();
		String found = cursor.getString(cursor.getColumnIndex("FOUND"));
		cursor.close();
		return found;
	}

	public String getSingleEntryDate(String itemName) {
		Cursor cursor = db.query("ITEM", null, "ITEMNAME=?",
				new String[] { itemName }, null, null, null);
		if (cursor.getCount() < 1) // UserName Not Exist
		{
			cursor.close();
			return "NOT EXIST";
		}
		cursor.moveToFirst();
		String date = cursor.getString(cursor.getColumnIndex("DATE"));
		cursor.close();
		return date;
	}
	public String getSingleEntryId(String itemName) {
		Cursor cursor = db.query("ITEM", null, "ITEMNAME=?",
				new String[] { itemName }, null, null, null);
		if (cursor.getCount() < 1) // UserName Not Exist
		{
			cursor.close();
			return "NOT EXIST";
		}
		cursor.moveToFirst();
		String id = cursor.getString(cursor.getColumnIndex("_id"));
		cursor.close();
		return id;
	}

	public String getSingleEntryImage(String itemName) {
		Cursor cursor = db.query("ITEM", null, "ITEMNAME=?",
				new String[] { itemName }, null, null, null);
		if (cursor.getCount() < 1) // UserName Not Exist
		{
			cursor.close();
			return "NOT EXIST";
		}
		cursor.moveToFirst();
		String date = cursor.getString(cursor.getColumnIndex("DATE"));
		cursor.close();
		return date;
	}
	public String getSingleEntryZipCode(String itemName) {
		Cursor cursor = db.query("ITEM", null, "ITEMNAME=?",
				new String[] { itemName }, null, null, null);
		if (cursor.getCount() < 1) // UserName Not Exist
		{
			cursor.close();
			return "NOT EXIST";
		}
		cursor.moveToFirst();
		String date = cursor.getString(cursor.getColumnIndex("ZIPCODE"));
		cursor.close();
		return date;
	}
	public ArrayList<String> getSingleEntryTags(String itemName) {
		Cursor cursor = db.query("ITEM", null, "ITEMNAME=?",
				new String[] { itemName }, null, null, null);
		if (cursor.getCount() < 1) // UserName Not Exist
		{
			cursor.close();
			return null;
		}
		cursor.moveToFirst();
		ArrayList<String> tags = new ArrayList<String>();
		tags.add(cursor.getString(cursor.getColumnIndex("BOOK")));
		tags.add(cursor.getString(cursor.getColumnIndex("ELECTRONIC")));
		tags.add(cursor.getString(cursor.getColumnIndex("PERSONAL")));
		//String date = cursor.getString(cursor.getColumnIndex(""));
		cursor.close();
		return tags;
	}
	
	public String getSingleEntryLatitude(String itemName) {
		Cursor cursor = db.query("ITEM", null, "ITEMNAME=?",
				new String[] { itemName }, null, null, null);
		if (cursor.getCount() < 1) // UserName Not Exist
		{
			cursor.close();
			return "NOT EXIST";
		}
		cursor.moveToFirst();
		String latitude = cursor.getString(cursor.getColumnIndex("LATITUDE"));
		cursor.close();
		return latitude;
	}
	
	public String getSingleEntryLongitude(String itemName) {
		Cursor cursor = db.query("ITEM", null, "ITEMNAME=?",
				new String[] { itemName }, null, null, null);
		if (cursor.getCount() < 1) // UserName Not Exist
		{
			cursor.close();
			return "NOT EXIST";
		}
		cursor.moveToFirst();
		String latitude = cursor.getString(cursor.getColumnIndex("LONGITUDE"));
		cursor.close();
		return latitude;
	}

	public void updateEntry(String userName, String itemName, String description, String date,
			String found, ArrayList<String> tags, Bitmap image, String zipCode, String tempItemName, String id, Bitmap imageFile) {
		// Define the updated row content.
		ContentValues updatedValues = new ContentValues();
		// Assign values for each row.
		updatedValues.put("USERNAME", userName);
		updatedValues.put("ITEMNAME", itemName);
		updatedValues.put("DESCRIPTION", description);
		updatedValues.put("DATE", date);
		updatedValues.put("FOUND", found);
		if(tags != null) {
			updatedValues.put("BOOK", tags.get(0));
			updatedValues.put("ELECTRONIC", tags.get(1));
			updatedValues.put("PERSONAL", tags.get(2));
		}
		updatedValues.put("ZIPCODE", zipCode);
		if(imageFile != null) {
			ByteArrayOutputStream stream = new ByteArrayOutputStream();
			Bitmap temp = imageFile;
			temp.compress(Bitmap.CompressFormat.PNG, 100, stream);
			byte[] byteArray = stream.toByteArray();
			updatedValues.put("IMAGE", byteArray);
		}
		//String where = getSingleEntryId(tempItemName);
		db.update("ITEM", updatedValues, "_id"+"="+id , null);
	}
	public void updateEntry(String userName, String itemName, String description, String date,
			String found, ArrayList<String> tags, Bitmap image, String zipCode, String tempItemName, String id) {
		// Define the updated row content.
		ContentValues updatedValues = new ContentValues();
		// Assign values for each row.
		updatedValues.put("USERNAME", userName);
		updatedValues.put("ITEMNAME", itemName);
		updatedValues.put("DESCRIPTION", description);
		updatedValues.put("DATE", date);
		updatedValues.put("FOUND", found);
		if(tags != null) {
			updatedValues.put("BOOK", tags.get(0));
			updatedValues.put("ELECTRONIC", tags.get(1));
			updatedValues.put("PERSONAL", tags.get(2));
		}
		updatedValues.put("ZIPCODE", zipCode);
		db.update("ITEM", updatedValues, "_id"+"="+id , null);
	}
	
	public void updateEntry(String latitude, String longitude, String id) {
		// Define the updated row content.
		ContentValues updatedValues = new ContentValues();
		// Assign values for each row.

		updatedValues.put("LATITUDE", latitude);
		updatedValues.put("LONGITUDE", longitude);
		db.update("ITEM", updatedValues, "_id"+"="+id , null);
	}
	
	
	public void updateEntry(String userName, String itemName, String description, String date,
			String found, ArrayList<String> tags, Bitmap image, String zipCode, String tempItemName, String id, String imageName) {
		// Define the updated row content.
		ContentValues updatedValues = new ContentValues();
		// Assign values for each row.
		updatedValues.put("USERNAME", userName);
		updatedValues.put("ITEMNAME", itemName);
		updatedValues.put("DESCRIPTION", description);
		updatedValues.put("DATE", date);
		updatedValues.put("FOUND", found);
		if(tags != null) {
			updatedValues.put("BOOK", tags.get(0));
			updatedValues.put("ELECTRONIC", tags.get(1));
			updatedValues.put("PERSONAL", tags.get(2));
		}
		updatedValues.put("ZIPCODE", zipCode);
		updatedValues.put("IMAGE", imageName);
		//String where = getSingleEntryId(tempItemName);
		db.update("ITEM", updatedValues, "_id"+"="+id , null);
	}

	public Cursor queueAll() {
		String[] columns = new String[] { "_id", "USERNAME", "ITEMNAME", "DESCRIPTION",
				"DATE", "FOUND", "BOOK", "ELECTRONIC", "PERSONAL", "ZIPCODE", "IMAGE"};
		Cursor cursor = db.query("ITEM", columns, null, null, null, null, null);
		return cursor;
	}

	public Cursor queueMatches(){
		Cursor lost = queueAllFiltered("NA", "LOST", null);
		Cursor found = queueAllFiltered("NA", "FOUND", null);

		ArrayList<String> columnArray1 = new ArrayList<String>();

		//columnArray1.add(cursor.getString(8));
		String[] columns = new String[] { "_id", "USERNAME", "ITEMNAME", "DESCRIPTION", 
				"DATE", "FOUND", "BOOK", "ELECTRONIC", "PERSONAL" , "ZIPCODE"};
		MatrixCursor mc = new MatrixCursor(columns);


		ArrayList<ArrayList<Integer>> indexes = new ArrayList<ArrayList<Integer>>();
		//count = 0;
		ArrayList<Integer> ints = new ArrayList<Integer>();
		for(int j = 0; j < lost.getCount(); j++) {
			lost.moveToPosition(j);
			//columnArray1.add(lost.getString(8));


			ints = new ArrayList<Integer>();
			ints.add(j);
			for (int i = 0; i < found.getCount(); i++) {
				found.moveToPosition(i);
				if (found.getString(8).equals(lost.getString(8))&&
						found.getString(2).toLowerCase().contains(lost.getString(2).toLowerCase())) {
					ints.add(i);
				}
			}
			indexes.add(ints);
		}
		for (int counter = 0; counter < indexes.size(); counter++) {
			if(indexes.get(counter).size()>1){
				lost.moveToPosition(indexes.get(counter).get(0));
				// for(int l = 0;l < cursor.getColumnCount(); l++ ){
				mc.addRow(new Object[] {
						lost.getLong(lost.getColumnIndex("_id")),
						lost.getString(lost.getColumnIndex("USERNAME")),
						lost.getString(lost.getColumnIndex("ITEMNAME")),
						lost.getString(lost.getColumnIndex("DESCRIPTION")),
						lost.getString(lost.getColumnIndex("DATE")),
						lost.getString(lost.getColumnIndex("FOUND")),
						lost.getString(lost.getColumnIndex("BOOK")),
						lost.getString(lost.getColumnIndex("ELECTRONIC")),
						lost.getString(lost.getColumnIndex("PERSONAL")),
						lost.getString(lost.getColumnIndex("ZIPCODE")) });
				// }
				for(int count = 1; count < indexes.get(counter).size(); count++){
					found.moveToPosition(indexes.get(counter).get(count));
					// for(int l = 0;l < cursor.getColumnCount(); l++ ){
					mc.addRow(new Object[] {
							found.getLong(found.getColumnIndex("_id")),
							found.getString(found.getColumnIndex("USERNAME")),
							found.getString(found.getColumnIndex("ITEMNAME")),
							found.getString(found.getColumnIndex("DESCRIPTION")),
							found.getString(found.getColumnIndex("DATE")),
							found.getString(found.getColumnIndex("FOUND")),
							found.getString(found.getColumnIndex("BOOK")),
							found.getString(found.getColumnIndex("ELECTRONIC")),
							found.getString(found.getColumnIndex("PERSONAL")),
							found.getString(found.getColumnIndex("ZIPCODE")) });
				}
			}
		}
		return mc;
	}


	public Cursor queueAllFiltered(String date, String found,
			ArrayList<String> category) {
		String[] columns = new String[] { "_id", "USERNAME", "ITEMNAME", "DESCRIPTION",
				"DATE", "FOUND", "DESCRIPTION","BOOK", "ELECTRONIC", "PERSONAL", "ZIPCODE" };
		Cursor cursor = db.query("ITEM", columns, null, null, null, null, null);

		MatrixCursor mc = new MatrixCursor(columns);
		// add extra rows, this will probably not work if you want to insert
		// them
		// between the initial cursor rows because of the _id column that need
		// autoincrement values
		// cursor.moveToFirst();

		ArrayList<String> columnArray1 = new ArrayList<String>();
		ArrayList<String> columnArray2 = new ArrayList<String>();
		ArrayList<String> columnArray3 = new ArrayList<String>();
		ArrayList<String> columnArray4 = new ArrayList<String>();
		ArrayList<String> columnArray5 = new ArrayList<String>();

		cursor.moveToFirst();
		columnArray1.add(cursor.getString(3));
		columnArray2.add(cursor.getString(4));
		columnArray3.add(cursor.getString(5));
		columnArray4.add(cursor.getString(6));
		columnArray5.add(cursor.getString(7));

		while (cursor.moveToNext()) {
			columnArray1.add(cursor.getString(3));
			columnArray2.add(cursor.getString(4));
			columnArray3.add(cursor.getString(5));
			columnArray4.add(cursor.getString(6));
			columnArray5.add(cursor.getString(7));
		}

		ArrayList<Integer> indexes = new ArrayList<Integer>();
		if (!date.equals("NA")) {
			for (int i = 0; i < columnArray1.size(); i++) {
				// Toast.makeText(, text, duration)
				if (columnArray1.get(i).substring(0, 7).equals(date)) {
					indexes.add(i);
				}
			}
		} else if (!found.equals("NA")) {
			if (found.equals("FOUND")) {
				for (int i = 0; i < columnArray2.size(); i++) {
					if (columnArray2.get(i).equals("true")) {
						indexes.add(i);
					}
				}
			}

			else if (found.equals("LOST")) {
				for (int i = 0; i < columnArray2.size(); i++) {
					if (columnArray2.get(i).equals("false")) {
						indexes.add(i);
					}// 0 to 7
				}
			}
		} else if (category != null) {
			if (category.get(0).equals("true")) {
				for (int i = 0; i < columnArray3.size(); i++) {
					if (columnArray3.get(i).equals("true")) {
						indexes.add(i);
					}
				}
			}
			if (category.get(1).equals("true")) {
				for (int i = 0; i < columnArray4.size(); i++) {
					if (columnArray4.get(i).equals("true")) {
						indexes.add(i);
					}
				}
			}
			if (category.get(2).equals("true")) {
				for (int i = 0; i < columnArray5.size(); i++) {
					if (columnArray5.get(i).equals("true")) {
						indexes.add(i);
					}
				}
			}

		}
		for (int i = 0; i < indexes.size(); i++) {
			cursor.moveToPosition(indexes.get(i));
			// for(int l = 0;l < cursor.getColumnCount(); l++ ){
			mc.addRow(new Object[] {
					cursor.getLong(cursor.getColumnIndex("_id")),
					cursor.getString(cursor.getColumnIndex("USERNAME")),
					cursor.getString(cursor.getColumnIndex("ITEMNAME")),
					cursor.getString(cursor.getColumnIndex("DESCRIPTION")),
					cursor.getString(cursor.getColumnIndex("DATE")),
					cursor.getString(cursor.getColumnIndex("FOUND")),
					cursor.getString(cursor.getColumnIndex("BOOK")),
					cursor.getString(cursor.getColumnIndex("ELECTRONIC")),
					cursor.getString(cursor.getColumnIndex("PERSONAL")),
					cursor.getString(cursor.getColumnIndex("ZIPCODE")) });
			// }

		}
		return mc;

	}

	public Cursor queueAllSearchQuery(String s) {
		if(!s.equals("")){
			String[] columns = new String[] { "_id", "USERNAME", "ITEMNAME", "DESCRIPTION",
					"DATE", "FOUND", "BOOK", "ELECTRONIC", "PERSONAL" };		
			Cursor cursor = db.query("ITEM", columns, null, null, null, null, null);
			ArrayList<String> columnArray1 = new ArrayList<String>();
			cursor.moveToFirst();
			columnArray1.add(cursor.getString(2));
			MatrixCursor mc = new MatrixCursor(columns);


			while (cursor.moveToNext()) {
				columnArray1.add(cursor.getString(2));

			}
			ArrayList<Integer> indexes = new ArrayList<Integer>();
			for (int i = 0; i < columnArray1.size(); i++) {
				// Toast.makeText(, text, duration)
				if (columnArray1.get(i).toLowerCase().contains(s.toLowerCase())) {
					indexes.add(i);
				}
			}
			for (int i = 0; i < indexes.size(); i++) {
				cursor.moveToPosition(indexes.get(i));
				// for(int l = 0;l < cursor.getColumnCount(); l++ ){
				mc.addRow(new Object[] {
						cursor.getLong(cursor.getColumnIndex("_id")),
						cursor.getString(cursor.getColumnIndex("USERNAME")),
						cursor.getString(cursor.getColumnIndex("ITEMNAME")),
						cursor.getString(cursor.getColumnIndex("DESCRIPTION")),
						cursor.getString(cursor.getColumnIndex("DATE")),
						cursor.getString(cursor.getColumnIndex("FOUND")),
						cursor.getString(cursor.getColumnIndex("BOOK")),
						cursor.getString(cursor.getColumnIndex("ELECTRONIC")),
						cursor.getString(cursor.getColumnIndex("PERSONAL")) });
				// }

			}
			return mc;
		}
		else{
			return null;
		}
	}

	public Cursor queueAllThisUser(String s) {
		String[] columns = new String[] { "_id", "USERNAME", "ITEMNAME", "DESCRIPTION",
				"DATE", "FOUND", "BOOK", "ELECTRONIC", "PERSONAL", "ZIPCODE" };	
		Cursor cursor = db.query("ITEM", columns, null, null, null, null, null);
		if(cursor.getCount()==0){
			return queueAll();
		}
		else{
			ArrayList<String> columnArray1 = new ArrayList<String>();
			cursor.moveToFirst();
			columnArray1.add(cursor.getString(1));
			MatrixCursor mc = new MatrixCursor(columns);


			while (cursor.moveToNext()) {
				columnArray1.add(cursor.getString(1));

			}
			ArrayList<Integer> indexes = new ArrayList<Integer>();
			for (int i = 0; i < columnArray1.size(); i++) {
				// Toast.makeText(, text, duration)
				if (s.equals(columnArray1.get(i))) {
					indexes.add(i);
				}
			}
			for (int i = 0; i < indexes.size(); i++) {
				cursor.moveToPosition(indexes.get(i));
				// for(int l = 0;l < cursor.getColumnCount(); l++ ){
				mc.addRow(new Object[] {
						cursor.getLong(cursor.getColumnIndex("_id")),
						cursor.getString(cursor.getColumnIndex("USERNAME")),
						cursor.getString(cursor.getColumnIndex("ITEMNAME")),
						cursor.getString(cursor.getColumnIndex("DESCRIPTION")),
						cursor.getString(cursor.getColumnIndex("DATE")),
						cursor.getString(cursor.getColumnIndex("FOUND")),
						cursor.getString(cursor.getColumnIndex("BOOK")),
						cursor.getString(cursor.getColumnIndex("ELECTRONIC")),
						cursor.getString(cursor.getColumnIndex("PERSONAL")),
						cursor.getString(cursor.getColumnIndex("ZIPCODE")) });
				// }

			}
			return mc;
		}
	}





	public Cursor queueAllLocationQuery(String s) {
		if(!s.equals("")){
			String[] columns = new String[] { "_id", "USERNAME", "ITEMNAME", "DESCRIPTION", 
					"DATE", "FOUND", "BOOK", "ELECTRONIC", "PERSONAL", "ZIPCODE" };		
			Cursor cursor = db.query("ITEM", columns, null, null, null, null, null);
			ArrayList<String> columnArray1 = new ArrayList<String>();
			cursor.moveToFirst();
			columnArray1.add(cursor.getString(8));
			MatrixCursor mc = new MatrixCursor(columns);


			while (cursor.moveToNext()) {
				columnArray1.add(cursor.getString(8));

			}
			ArrayList<Integer> indexes = new ArrayList<Integer>();
			for (int i = 0; i < columnArray1.size(); i++) {
				// Toast.makeText(, text, duration)
				if (columnArray1.get(i).contains(s)) {
					indexes.add(i);
				}
			}
			for (int i = 0; i < indexes.size(); i++) {
				cursor.moveToPosition(indexes.get(i));
				// for(int l = 0;l < cursor.getColumnCount(); l++ ){
				mc.addRow(new Object[] {
						cursor.getLong(cursor.getColumnIndex("_id")),
						cursor.getString(cursor.getColumnIndex("USERNAME")),
						cursor.getString(cursor.getColumnIndex("ITEMNAME")),
						cursor.getString(cursor.getColumnIndex("DESCRIPTION")),
						cursor.getString(cursor.getColumnIndex("DATE")),
						cursor.getString(cursor.getColumnIndex("FOUND")),
						cursor.getString(cursor.getColumnIndex("BOOK")),
						cursor.getString(cursor.getColumnIndex("ELECTRONIC")),
						cursor.getString(cursor.getColumnIndex("PERSONAL")),
						cursor.getString(cursor.getColumnIndex("ZIPCODE")) });
				// }

			}
			return mc;
		}
		else{
			return null;
		}
	}


	public ArrayList<String> queueAllItems() {
		String[] columns = new String[] { "ITEMNAME" };
		Cursor cursor = db.query("ITEM", columns, null, null, null, null, null);
		ArrayList<String> result = new ArrayList<String>();

		int index_CONTENT = cursor.getColumnIndex("ITEMNAME");
		for (cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()) {
			result.add(cursor.getString(index_CONTENT));
		}
		return result;
	}
}
